<?php

namespace App\Observers;

use App\Models\Common\Channel\Channel;
use Illuminate\Support\Facades\Cache;

class ChannelObserver
{
    public function creating(Channel $channel)
    {

        $channel->app_key = '';
        $channel->iv      = '';
    }

    /**
     * Handle the User "created" event.
     *
     * @param Channel $channel
     * @return void
     */
    public function created(Channel $channel)
    {
        $cache_key = "Channel:id:{$channel->channel_id}";
        Cache::forever($cache_key, $channel);

        $cache_key = "Channel:appid:{$channel->appid}";
        Cache::forever($cache_key, $channel);
    }

    public function updated(Channel $channel)
    {
        $cache_key = "Channel:id:{$channel->channel_id}";
        Cache::forever($cache_key, $channel);

        $cache_key = "Channel:appid:{$channel->appid}";
        Cache::forever($cache_key, $channel);
    }

    public function saved(Channel $channel)
    {
        $cache_key = "Channel:id:{$channel->channel_id}";
        Cache::forever($cache_key, $channel);

        $cache_key = "Channel:appid:{$channel->appid}";
        Cache::forever($cache_key, $channel);
    }


    /**
     * Handle the User "deleted" event.
     *
     * @param Channel $channel
     * @return void
     */
    public function deleted(Channel $channel)
    {
        $cache_key = "Channel:id:{$channel->channel_id}";
        Cache::forget($cache_key);

        $cache_key = "Channel:appid:{$channel->appid}";
        Cache::forget($cache_key);
    }
}
